<template>
	<view>
		<view class="speck">
			<view class="head">
				<view class="img">
					<view v-for="(item, i) in tempFilePaths" :key="i" style="position: relative;">
						<image :class="[(i+1)%4 === 0 ?'noMargin':'']" :src="item" @click="previewImage(i)" mode="">
						</image>
						<image :class="[(i+1)%4 === 0 ?'noMargindel':'']" class="del" src="@/static/mains/del.png"
							mode="" @click="delImg(i)"></image>
					</view>
					<view class="upload" @click="upload"></view>
				</view>
				
				<view class="inputWrap">
					<u--textarea class="textarea" height="180" border="none" v-model="content" placeholder="展开说说对套餐的体验吧..." count maxlength="300"></u--textarea>
				</view>
			</view>
		</view>
		<view class="say">
			<view class="title">评价</view>
			<view>
				<u-rate @change="change" v-model="value" activeColor="#C65B52" size="30"></u-rate>
			</view>
		</view>

		<view @click="submit" class="other-button" hover-class="other-button-hover">提交</view>
	</view>
</template>

<script>
	import { uploadFile } from '@/utils/tools';
	export default {
		data() {
			return {
				id: '',
				tempFilePaths: [
					// '@/static/mains/m1.png',
					// '@/static/mains/m1.png',
					// '@/static/mains/m1.png',
				],
				content: '',
				value: 1
			}
		},
		onLoad(options) {
			uni.setNavigationBarColor({
				frontColor: '#000000',
				backgroundColor: '#E2867E'
			})
			this.id = options.id;
		},
		methods: {
			change(e){
				// console.log(e)
				this.value = e;
			},
			// 上传
			upload() {
				var that = this;
				uni.chooseImage({
					count: 3, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function (res) {
						let list = [];
						let count = 0;
						let files = res.tempFilePaths;
						uni.showLoading();
						files.forEach((item) => {
							uploadFile(item, {
								header: {
									'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
								}
							})
								.then((res) => {
									uni.hideLoading();
									count++;
									//如果是最后一张,则隐藏等待中
									if (count == files.length) {
										wx.hideLoading();
									}
									// console.log(res,'1111');
									// this.avatar = res;
									list.push(res); //待上传图片临时路径
									that.tempFilePaths = list;
								})
								.catch(() => {
									uni.hideLoading();
									uni.showToast({
										title: '上传失败,请重新上传!',
										icon: 'none'
									});
								});
						});
					}
				});
			},
			// 删除
			delImg(i) {
				this.tempFilePaths.splice(i, 1);
			},
			// 预览
			previewImage: function(index) {
				let imgs = []
				for(let i=0;i<this.tempFilePaths.length;i++){
					let str = this.tempFilePaths[i]
					imgs.push(str)
				}
				uni.previewImage({
					current: index, //预览图片的下标
					urls: imgs //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},
			async submit(){
				if(!this.content){
					uni.showToast({
						title:'请输入您的体验',
						icon:'none'
					})
					return
				}
				let params ={
					order_goods_id:this.id,
					comment:this.content,
					goods_comment:this.value,
					service_comment:5,
					description_comment:5,
					express_comment:5,
					image:this.tempFilePaths.toString()
				}
				let res = await this.$api.orderadd(params);
				if(res.code == 1){
					// console.log(res)
					uni.showToast({
						title:'操作成功'
					})
					setTimeout(function(){
						uni.reLaunch({
							url:'/pages/mains/order/order?type=finish'
						})
					},1000)
				}
			},
		}
	}
</script>

<style scoped lang="less">
	.speck {
		background: url('https://z1.ax1x.com/2023/09/14/pPR7xYj.png') no-repeat;
		background-size: 100% 100%;
		width: 100%;
		height: 700rpx;

		.head {
			padding:25rpx;
			.inputWrap{
				margin-top: 50rpx;
				background: #fff;
				border-radius: 25rpx;
		
				.textarea{
					width: 100%;
					background: #fff;
					padding: 30rpx;
					border-radius: 25rpx;
					border: none;
				}
			}
			.img {
				display: flex;
				flex-wrap: wrap;
				background: #fff;
				padding: 30rpx;
				border-radius: 25rpx;
				.del {
					width: 40rpx;
					height: 40rpx;
					position: absolute;
					top: -20rpx;
					right: -20rpx;
				}
			}
			
			image {
				width: 140rpx;
				height: 140rpx;
				margin-bottom: 20rpx;
				margin-right: 26rpx;
			}
			
			.noMargin {
				margin-right: 0rpx;
			}
			
			.noMargindel {
				top: -20rpx !important;
				right: -44rpx !important;
			}
			
			.upload {
				width: 140rpx;
				height: 140rpx;
				background: url(@/static/mains/m8.png) no-repeat;
				background-size: 100% 100%;
				margin-bottom: 20rpx;
			}

		}
	}
	.say{
		background: #fff;
		padding: 30rpx;
		border-radius: 25rpx;
		margin:60rpx 30rpx 0 30rpx;
		
		.title{
			font-size: 30rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
	}
</style>